package com.ymatou.iapi.userservice.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.ymatou.iapi.login.parameter.BindWeiXinUseroRequestDtoBean;
import com.ymatou.iapi.login.parameter.WeiXinUserRequestDtoBean;
import com.ymatou.iapi.login.service.BindWeiXinUseroRequestDtoCall;
import com.ymatou.iapi.login.service.WeiXinUserRequestDtoCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.QueryBatchGlobalWeiXinInfoBean;
import com.ymatou.iapi.userservice.parameter.enums.AppSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ChannelSourceEnum;
import com.ymatou.iapi.userservice.parameter.enums.ThirdPartyTypeEnum;
import com.ymatou.iapi.userservice.parameter.req.BindThirdPartyUserReq;
import com.ymatou.iapi.userservice.parameter.req.CreateThirdPartyUserReq;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymatou.iapi.userservice.parameter.req.SetUserLogoReq;
import com.ymatou.iapi.userservice.parameter.resp.UserInfo2;
import com.ymatou.iapi.userservice.service.CreateThirdPartyUserCall;
import com.ymatou.iapi.userservice.service.QueryBatchGlobalWeiXinInfoCall;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.utils.TestDataManager;
/**
*批量查询微信信息优选专用
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryBatchGlobalWeiXinInfo{
	private static final String GetLogoUrl = null;
	private static QueryBatchGlobalWeiXinInfoBean querybatchglobalweixininfoBean;
	private static QueryBatchGlobalWeiXinInfoCall querybatchglobalweixininfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量查询微信信息优选专用");
	}

	@Before
	public void caseUp() {
		querybatchglobalweixininfoBean=new QueryBatchGlobalWeiXinInfoBean();
		querybatchglobalweixininfoCall=new QueryBatchGlobalWeiXinInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(querybatchglobalweixininfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryBatchGlobalWeiXinInfo_001() {
		Logger.start(true,"批量查询微信信息优选专用查询成功");
		try {
			//创建新用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			//获取用户信息然后绑定ymt
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalWxWap);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			//传入userid，查询消息
			List<Integer> userIdList=new  ArrayList<Integer>();
			userIdList.add(userId);
			querybatchglobalweixininfoBean.setUserIdList(userIdList);
			querybatchglobalweixininfoCall.setData(querybatchglobalweixininfoBean);
			querybatchglobalweixininfoCall.callService();
			Logger.verifyEquals("200",""+querybatchglobalweixininfoCall.getStatusCode(),"判断返回值");
			checkUser(querybatchglobalweixininfoCall.getJsonObject());
			
			checkPic(querybatchglobalweixininfoCall.getJsonObject(), 1);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryBatchGlobalWeiXinInfo_002() {
		Logger.start(true, "使用微信新用户联登，获取用户头像");
		try {
			CreateThirdPartyUserReq createThirdPartyUserReq = JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.WeiXin);
			createThirdPartyUserReq.setAppSource(AppSourceEnum.GlobalApp);
			createThirdPartyUserReq
					.setHeadimgurl("http://wx.qlogo.cn/mmopen/uI5pczeERTY36SclJhUtdDRPN6adUrxvjIWtYyK7MtM58zHxYqcbk8hFib2mXic2rTocOlI4ib43QUx1iayZDzJxoicVR7ibiaqKqGn/0");
			CreateThirdPartyUserCall createThirdPartyUserCall = new CreateThirdPartyUserCall();
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid = createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();

			//传入userid，查询消息
			List<Integer> userIdList=new  ArrayList<Integer>();
			userIdList.add(uid);
			querybatchglobalweixininfoBean.setUserIdList(userIdList);
			querybatchglobalweixininfoCall.setData(querybatchglobalweixininfoBean);
			querybatchglobalweixininfoCall.callService();
			Logger.verifyEquals("200",""+querybatchglobalweixininfoCall.getStatusCode(),"判断返回值");
			checkUser(querybatchglobalweixininfoCall.getJsonObject());
			
			checkPic(querybatchglobalweixininfoCall.getJsonObject(), 3);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryBatchGlobalWeiXinInfo_003() {
		Logger.start(true, "使用微信新用户联登，然后修改用户头像，再获取用户头像");
		try {
			CreateThirdPartyUserReq createThirdPartyUserReq = JUserCallService.initCreateThirdUserReq();
			createThirdPartyUserReq.setThirdPartyType(ThirdPartyTypeEnum.WeiXin);
			createThirdPartyUserReq.setChannelSource(ChannelSourceEnum.WeiXin);
			createThirdPartyUserReq.setAppSource(AppSourceEnum.GlobalApp);
			createThirdPartyUserReq
					.setHeadimgurl("http://wx.qlogo.cn/mmopen/uI5pczeERTY36SclJhUtdDRPN6adUrxvjIWtYyK7MtM58zHxYqcbk8hFib2mXic2rTocOlI4ib43QUx1iayZDzJxoicVR7ibiaqKqGn/132");
			CreateThirdPartyUserCall createThirdPartyUserCall = new CreateThirdPartyUserCall();
			createThirdPartyUserCall.setData(createThirdPartyUserReq);
			createThirdPartyUserCall.callService();
			Logger.verifyEquals("true", createThirdPartyUserCall.getString("Success"), "验证Success");
			Logger.verifyEquals("200", createThirdPartyUserCall.getString("ResponseCode"), "验证ResponseCode");
			Integer uid = createThirdPartyUserCall.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			
			//修改头像
			JUserCallService.SetUserLogoCall(uid, "http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792");

			//传入userid，查询消息
			List<Integer> userIdList=new  ArrayList<Integer>();
			userIdList.add(uid);
			querybatchglobalweixininfoBean.setUserIdList(userIdList);
			querybatchglobalweixininfoCall.setData(querybatchglobalweixininfoBean);
			querybatchglobalweixininfoCall.callService();
			
			Logger.verifyEquals("200",""+querybatchglobalweixininfoCall.getStatusCode(),"判断返回值");
			checkUser(querybatchglobalweixininfoCall.getJsonObject());
			checkPic(querybatchglobalweixininfoCall.getJsonObject(), 4);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryBatchGlobalWeiXinInfo_004() {
		Logger.start(true, "老用户、绑定微信，然后修改用户头像。再获取用户头像");
		try {
			//创建新用户
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			int uid=urrc.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
			//修改头像
			JUserCallService.SetUserLogoCall(uid, "http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792");
			//获取用户信息然后绑定ymt
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			Integer userId=registerResult.get("UserId").getAsInt();
			BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
			bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalWxWap);
			BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
			
			
			//传入userid，查询消息
			List<Integer> userIdList=new  ArrayList<Integer>();
			userIdList.add(userId);
			querybatchglobalweixininfoBean.setUserIdList(userIdList);
			querybatchglobalweixininfoCall.setData(querybatchglobalweixininfoBean);
			querybatchglobalweixininfoCall.callService();
			Logger.verifyEquals("200",""+querybatchglobalweixininfoCall.getStatusCode(),"判断返回值");
			checkUser(querybatchglobalweixininfoCall.getJsonObject());
			checkPic(querybatchglobalweixininfoCall.getJsonObject(),2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QueryBatchGlobalWeiXinInfo_005() {
		Logger.start(true, "多个用户一起查询");
		try {
			
			List<Integer> userIdList=new  ArrayList<Integer>();
			for (int i = 0; i < 10; i++) {
				//创建新用户
				UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
				UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
				int uid=urrc.getJsonObject().get("Result").getAsJsonObject().get("UserId").getAsInt();
				//修改头像
				JUserCallService.SetUserLogoCall(uid, "http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792");
				//获取用户信息然后绑定ymt
				JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
				String token=registerResult.get("Token").getAsString();
				Integer userId=registerResult.get("UserId").getAsInt();
				BindWeiXinUseroRequestDtoBean bean=JUserCallService.initBindWeiXinUseroRequestDtoBean(token);
				bean.getWeiXinUser().setRegSource(AppSourceEnum.GlobalWxWap);
				BindWeiXinUseroRequestDtoCall wx = JUserCallService.bindWeiXinUseroRequestDto(bean);
				userIdList.add(userId);
			}
			
			querybatchglobalweixininfoBean.setUserIdList(userIdList);
			querybatchglobalweixininfoCall.setData(querybatchglobalweixininfoBean);
			querybatchglobalweixininfoCall.callService();
			Logger.verifyEquals("200",""+querybatchglobalweixininfoCall.getStatusCode(),"判断返回值");
			checkUser(querybatchglobalweixininfoCall.getJsonObject());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * flag 1->未修改变更头像
	 * 		2->已经修改头像
	 * 		3->微信联登
	 * 		4->联登修改
	 * @param jsonObject
	 * @param flag
	 */
	public static void checkPic(JsonObject jsonObject,int flag){
		JsonArray asJsonArray = jsonObject.get("Data").getAsJsonArray();
		String defaultWx="http://wx.qlogo.cn/mmopen/uI5pczeERTY36SclJhUtdDRPN6adUrxvjIWtYyK7MtM58zHxYqcbk8hFib2mXic2rTocOlI4ib43QUx1iayZDzJxoicVR7ibiaqKqGn/132";
		String defaultYmt="http://staticontent.ymatou.com/app/userlogo/mj.jpg";
		String updateYmt="http://file.iapi.ymatou.com/G01/M00/1F/55/rBBlD11nhWKAOjQzAABaeGeA9IE1416792";
		asJsonArray.forEach(b->{
			String logoUrl = b.getAsJsonObject().get("logoUrl").toString().replace("\"", "");
			String wxLogoUrl = b.getAsJsonObject().get("wxLogoUrl").toString().replace("\"", "");
			if(flag==1){
				Logger.verifyEquals(logoUrl,defaultYmt, "默认洋码头头像判断1");
				Logger.verifyEquals(wxLogoUrl,"Headimgurl", "没有微信头像判断1");
			}else if(flag==2){//
				Logger.verifyEquals(logoUrl,updateYmt, "修改洋码头头像判断2");
				Logger.verifyEquals(wxLogoUrl,"Headimgurl", "没有微信头像判断2");
			}else if (flag==3) {//直接联登，微信与洋码头都是微信头像
				Logger.verifyEquals(logoUrl,wxLogoUrl, "微信联登头像判断3");
				Logger.verifyEquals(logoUrl,defaultWx, "微信联登头像判断3");
				Logger.verifyEquals(wxLogoUrl,defaultWx, "微信联登头像判断3");
			}else if(flag==4){//联登修改后，微信头像还是默认，洋码头头像更改
				Logger.verifyEquals(logoUrl,updateYmt, "微信联登修改头像判断4");
				Logger.verifyEquals(wxLogoUrl,defaultWx, "微信联登修改头像判断4");
			}
		});
	}
	
	public static void checkUser(JsonObject jsonObject){
		JsonArray asJsonArray = jsonObject.get("Data").getAsJsonArray();
		asJsonArray.forEach(b->{
			int asInt = b.getAsJsonObject().get("userId").getAsInt();
			try {
				UserInfo2 getUserInfo = JUserCallService.GetUserInfo(asInt);
				JsonObject wxInfo = JUserCallService.SingleWeiXinBriefInfoCall(asInt, 0);
				Logger.verifyEquals(getUserInfo.getUserId(), asInt, "判断用户id");
				Logger.verifyEquals(getUserInfo.getLoginMobile().toString(),b.getAsJsonObject().get("loginMobile").toString().replace("\"",""), "判断用户手机号");
				Logger.verifyEquals(getUserInfo.getLoginId().toString(),b.getAsJsonObject().get("loginId").toString().replace("\"",""), "判断用户登录id");
				Logger.verifyEquals(wxInfo.get("LogoUrl").toString(),b.getAsJsonObject().get("logoUrl").toString(), "判断用户头像");
				Logger.verifyEquals(wxInfo.get("WeiXinNickName").toString(),b.getAsJsonObject().get("wxNickName").toString(), "判断用户微信名称");
				Logger.verifyEquals(wxInfo.get("WeiXinLogoUrl").toString(),b.getAsJsonObject().get("wxLogoUrl").toString(), "判断用户微信头像");
			} catch (Exception e) {
				Logger.fail(e);
			}
		});
	}
	
	
	
	
}